package com.yuanchu.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.media.dao.YoshopUploadGroupDao;
import com.yuanchu.media.dto.EditFileGroupParamsDto;
import com.yuanchu.media.dto.EditFileParamsDto;
import com.yuanchu.media.dto.MoveGroupDto;
import com.yuanchu.media.entity.YoshopUploadFileEntity;
import com.yuanchu.media.entity.YoshopUploadGroupEntity;
import com.yuanchu.media.service.YoshopUploadFileService;
import com.yuanchu.media.service.YoshopUploadGroupService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("yoshopUploadGroupService")
public class YoshopUploadGroupServiceImpl extends ServiceImpl<YoshopUploadGroupDao, YoshopUploadGroupEntity> implements YoshopUploadGroupService {

    @Resource
    private YoshopUploadGroupDao yoshopUploadGroupDao;
    @Resource
    private YoshopUploadFileService uploadFileService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YoshopUploadGroupEntity> page = this.page(
                new Query<YoshopUploadGroupEntity>().getPage(params),
                new QueryWrapper<YoshopUploadGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<YoshopUploadGroupEntity> queryGroupList() {
        List<YoshopUploadGroupEntity> list = this.list();
        list.forEach(item -> {
            item.setCreateTime(DateFormatUtils.formatDatetime(item.getCreateTime()));
            item.setUpdateTime(DateFormatUtils.formatDatetime(item.getUpdateTime()));
        });
        List<YoshopUploadGroupEntity> collect = list.stream()
                .filter(item -> item.getParentId() == 0)
                .map(item -> {
                    item.setChildren(getChildren(item, list));
                    return item;
                }).sorted(Comparator.comparingInt(YoshopUploadGroupEntity::getSort)).collect(Collectors.toList());
        return collect;
    }

    private List<YoshopUploadGroupEntity> getChildren(YoshopUploadGroupEntity item, List<YoshopUploadGroupEntity> list) {
        List<YoshopUploadGroupEntity> children = list.stream().filter(subItem -> subItem.getParentId().equals(item.getGroupId()))
                .map(subItem -> {
                    subItem.setChildren(getChildren(subItem, list));
                    return subItem;
                }).sorted(Comparator.comparingInt(YoshopUploadGroupEntity::getSort))
                .collect(Collectors.toList());
        return children;
    }

    @Override
    public void addGroup(EditFileGroupParamsDto param) {

        YoshopUploadGroupEntity form = param.getForm();
        form.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        form.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        form.setStoreId(10001);
        this.save(form);
    }

    @Override
    public void editGroup(EditFileGroupParamsDto form) {
        Integer groupId = form.getGroupId();
        YoshopUploadGroupEntity groupEntity = form.getForm();
        groupEntity.setGroupId(groupId);
        groupEntity.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));

        this.updateById(groupEntity);
    }

    @Override
    public void moveGroup(MoveGroupDto params) {
        List<YoshopUploadFileEntity> files = uploadFileService.list(new QueryWrapper<YoshopUploadFileEntity>().in("file_id", params.getFileIds()));
        if (CollectionUtils.isEmpty(files)) {
            return;
        }
        files.forEach(item -> {
            item.setGroupId(params.getGroupId());
            item.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
            uploadFileService.updateById(item);
        });
    }

}